

export default {
    namespace: 'mine',
    state: {
        userInfo: {}, //用户基本信息
        userData: {}, // 用户数据
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign(state, payload);
        },
    },
    effects: {
        *init({ }, { put }){
            const userData = yield put.resolve({
                type: '$request/post',
                payload: {
                    url: '/api/statistic',
                    data: {}
                }
            });
            yield put({ type: 'save', payload: {userData} });
            yield put({ type : 'getUserInfo'});
        },
        /**
         * 获取用户信息
         * @param {*} param0 
         * @param {*} param1 
         */
        * getUserInfo({ }, { put }) {
            const userInfo = yield put.resolve({
                type: '$request/post',
                payload: {
                    url : '/sso/getUserInfo'
                }
            });
            yield put({ type: 'save', payload: {
                userInfo
            } });
            return userInfo;
        },
        /**
         * 更新用户信息
         * @param {*} param0 
         * @param {*} param1 
         */
        *updateUserInfo({payload }, { put }){
            yield put.resolve({
                type: '$request/post',
                payload: {
                    url: '/sso/updateUserInfo',
                    data: payload
                }
            });
            yield put({
                type: 'getUserInfo'
            });
        },
        
        /**
         * 更新用户积分
         * @param {*} param0 
         * @param {*} param1 
         */
        *updateIntegral({payload }, { put,select }){
            const integral =  yield put.resolve({
                type: '$request/post',
                payload: {
                    url: '/sso/addIntegral',
                    data: payload
                }
            });

            const {userInfo} = yield select(s => s.mine);
            yield put({ type: 'save', payload: {
                userInfo : {...userInfo, integral}
            } });
        },
    }
};